package com.tubang.logistics.controller.orderV2;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.BeanMapper;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.ResultData;
import com.tubang.component.util.RequestUtil;
import com.tubang.logistics.entity.PlatformOrderBiddingEntity;
import com.tubang.logistics.service.IPlatformOrderBiddingService;
import com.tubang.logistics.vo.OrderBiddingRefuseVo;
import com.tubang.logistics.vo.OrderBiddingVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

/**
 * <p>
 * 平台竞价表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2018-01-15
 */
@RestController
@RequestMapping("/orderBidding")
public class OrderBiddingController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private IPlatformOrderBiddingService platformOrderBiddingService;


    /**
     * 获取订单竞价列表
     *
     * @return
     */
    @GetMapping(value = "getOrderBiddinglISTByOrderId")
    public ResultData getOrderBiddinglISTByOrderId(@RequestParam(required = true) String orderId,
                                                   @RequestParam(required = false) String userName,
                                                   @RequestParam(required = false, defaultValue = "1") Integer page,
                                                   @RequestParam(required = false, defaultValue = "10") Integer rows) {
        try {
            EntityWrapper<PlatformOrderBiddingEntity> entityWrapper = new EntityWrapper<PlatformOrderBiddingEntity>();
            entityWrapper.orderBy(true, "carriage");
            entityWrapper.eq("order_id", orderId);
            entityWrapper.eq("enable", Constants.DEFAULT_VAULE_ONE);
            if (StringUtils.isNotBlank(userName)) {
                entityWrapper.eq("carrier", userName);
            }
            Page<PlatformOrderBiddingEntity> pageList = platformOrderBiddingService.selectPage(new Page<PlatformOrderBiddingEntity>(page, rows), entityWrapper);
            List<PlatformOrderBiddingEntity> records = pageList.getRecords();
            List<OrderBiddingVo> platformOrderBiddingVos = BeanMapper.mapList(records, OrderBiddingVo.class);
            int count = platformOrderBiddingService.selectCount(entityWrapper);
            return ResultData.result(true).setData(platformOrderBiddingVos).setTotalCount(count);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[orderBidding]==> method[getOrderBiddinglISTByOrderId],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }


    /**
     * 竞价
     *
     * @return
     */
    @PostMapping(value = "biddingOrder")
    public ResultData biddingOrder(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);
            OrderBiddingVo platformOrderBiddingVo = GsonQuick.toObjectDate(receivePostJson, OrderBiddingVo.class);
            Timestamp now = new Timestamp(System.currentTimeMillis());
            //先将之前的标记删除

            platformOrderBiddingService.deleteByCarrierIdAndOrderId(platformOrderBiddingVo.getCarrierId(),platformOrderBiddingVo.getOrderId());

            PlatformOrderBiddingEntity pfob = BeanMapper.map(platformOrderBiddingVo, PlatformOrderBiddingEntity.class);
            pfob.setStatusCode(Constants.ORDER_BIDD_ING);
            pfob.setStatusCodeDesc("竞价中");
            pfob.setCreateTime(now);
            pfob.setUpdateTime(now);
            pfob.setEnable(Boolean.TRUE);
            platformOrderBiddingService.insertOrUpdate(pfob);
            return ResultData.result(true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[orderBidding]==> method[biddingOrder],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }


    /**
     * 拒绝竞价
     *
     * @return
     */
    @PostMapping(value = "refuseBiddingOrder")
    public ResultData refuseBiddingOrder(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);
            OrderBiddingRefuseVo platformOrderBiddingVo = GsonQuick.toObjectDate(receivePostJson, OrderBiddingRefuseVo.class);
            Timestamp now = new Timestamp(System.currentTimeMillis());
            List<String> ids = platformOrderBiddingVo.getIds();
            for (String id : ids) {
                PlatformOrderBiddingEntity platformOrderBiddingEntity = platformOrderBiddingService.selectById(id);
                if (platformOrderBiddingEntity == null) {
                    return ResultData.result(true);
                }
                platformOrderBiddingEntity.setStatusCode(Constants.ORDER_BIDD_NO);
                platformOrderBiddingEntity.setStatusCodeDesc("竞价失败");
                platformOrderBiddingEntity.setRemarks(platformOrderBiddingVo.getRemarks());
                platformOrderBiddingEntity.setUpdateTime(now);
                platformOrderBiddingService.insertOrUpdate(platformOrderBiddingEntity);
            }

            return ResultData.result(true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[orderBidding]==> method[refuseBiddingOrder],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }

}
